package com.machine.filling.util;

import android.util.Log;

public class HexUtils {
    /**
     * 对double 类型 保留point位小数
     *
     * @param d
     * @param point
     * @return
     */
    public static String formatDouble(double d, int point) {
        String df = String.format("%." + point + "f", d);
        return df;
    }

    /**
     * 字符串长度补齐length位, 如果不够 前位补0
     *
     * @param str
     * @param length
     * @return
     */
    public static String coverStrFromFirst(String str, int length) {
        int count = length - str.length();
        for (int i = 0; i < count; i++) {
            str = "0" + str;
        }
        return str;
    }

    /**
     * 303132 ----> 023
     *
     * @param s
     * @return
     */
    public static String ASCII2Str(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "ASCII");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    /**
     * (123,4) --> 30 313233(长度不足 前位补30)
     * (2,5) -->  3030303032
     *
     * @param str
     * @param length
     * @return
     */
    public static String str2ASCII(String str, int length) {
        int res = length - str.length();
        if (res > 0) {
            for (int i = 0; i < res; i++) {
                str = "0" + str;
            }
        }
        char[] chars = str.toCharArray();

        StringBuffer hex = new StringBuffer();
        for (int i = 0; i < length; i++) {
            hex.append(Integer.toHexString((int) chars[i]));
        }
        return hex.toString();
    }

    /**
     * 字符串转换成为16进制(无需Unicode编码)  可以将汉字转码成16进制
     * @param str
     * @return
     */
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * 16进制直接转换成为字符串(无需Unicode解码)  可以将16进制转码成汉字
     * @param hexStr
     * @return
     */
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }


    /**
     * double 转16进制
     *
     * @param dou
     * @param hexSize 字节长度
     * @return
     */
    public static String double2Hex(double dou, int hexSize) {
        String hex = Long.toHexString(Double.doubleToRawLongBits(dou));
        if (hex.length() % 2 != 0) {
            hex = "0" + hex;
        }
        int lastLength = hexSize - hex.length() / 2;

        for (int i = 0; i < lastLength; i++) {
            hex = "" + hex;
        }
        return hex;
    }

    /**
     * int 转16进制
     *
     * @param in
     * @param hexSize 字节长度
     * @return
     */
    public static String int2Hex(int in, int hexSize) {
        String hex = Integer.toHexString(in);
        if (hex.length() % 2 != 0) {
            hex = "0" + hex;
        }
        int lastLength = hexSize - hex.length() / 2;
        for (int i = 0; i < lastLength; i++) {
            hex = "00" + hex;
        }
        return hex;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println();

    }

    /**
     * 16进制字符串转int
     *
     * @param hex
     * @return
     */
    public static int hex2Int(String hex) {
        if (hex.length() > 8) {
            Log.e("HexUtils", "hex2Int: 报错了-----------");
            return 0;
        }
        return Integer.valueOf(hex, 16);
    }

    public void test() {
    }
}
